/*
 * ===================================================================
 *  TS 26.104
 *  REL-5 V5.4.0 2004-03
 *  REL-6 V6.1.0 2004-03
 *  3GPP AMR Floating-point Speech Codec
 * ===================================================================
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include "typedef.h"
#include "interf_enc.h"

#define AMR_MAGIC_NUMBER "#!AMR\n"

static const short modeConv[]={
    475, 515, 59, 67, 74, 795, 102, 122
};

static void Usage(char* argv[])
{
    fprintf (stderr,
             "Usage of %s:\n\n"
             "[-dtx] mode speech_file bitstream_file \n\n"
             "or \n\n"
             "[-dtx] -modefile=mode_file speech_file bitstream_file \n\n\n"
             "mode = MR475, MR515, MR59, MR67, MR74, MR795, MR102, MR122 \n\n\n",
             argv[0]);
}

int main (int argc, char * argv[]) {

    /* file strucrures */
    FILE * file_speech = NULL;
    FILE * file_encoded = NULL;
    FILE * file_mode = NULL;

    /* input speech vector */
    short speech[160];

    /* counters */
    int byte_counter, frames = 0, bytes = 0;

    /* pointer to encoder state structure */
    int *enstate;

    /* requested mode */
    enum Mode req_mode = MR122;
    int dtx = 0;

    /* temporary variables */
    char mode_string[9];
    long mode_tmp;

    /* bitstream filetype */
    unsigned char serial_data[32];

    /* Process command line options */

    if ((argc == 5) || (argc == 4)) {
        file_encoded = fopen(argv[argc - 1], "wb");
        if (file_encoded == NULL) {
            Usage(argv);
            return 1;
        }
        file_speech = fopen(argv[argc - 2], "rb");
        if (file_speech == NULL) {
            fclose(file_encoded);
            Usage(argv);
            return 1;
        }
        if (strncmp(argv[argc - 3], "-modefile=", 10) == 0) {
            file_mode = fopen(&argv[argc - 3][10], "rt");
            if (file_mode == NULL) {
                Usage(argv);
                fclose(file_speech);
                fclose(file_encoded);
                return 1;
            }
        }
        else {
            mode_tmp = strtol(&argv[argc - 3][2], NULL, 0);
            for (req_mode = 0; req_mode < 8; req_mode++) {
                if (mode_tmp == modeConv[req_mode])
                    break;
            }
            if (req_mode == 8) {
                Usage(argv);
                fclose(file_speech);
                fclose(file_encoded);
                if (file_mode != NULL)
                    fclose(file_mode);
                return 1;
            }
        }
        if (argc == 5) {
            if ((strcmp(argv[1], "-dtx") != 0)) {
                Usage(argv);
                fclose(file_speech);
                fclose(file_encoded);
                if (file_mode != NULL) {
                    fclose(file_mode);
                }
                return 1;
            }
            else {
                dtx = 1;
            }
        }
    }
    else {
        Usage(argv);
        return 1;
    }
    if(1){
    	int i=0;
        printf("\ndtx=%d,argc=%d\n",dtx,argc);
		for(;i<argc;i++){
			char * p = argv[i];
			printf("argv[%d]=%s\n",i,p);
		}
    }
	/*----------------------------------------------------------------------------------------------------------------------------*/

    enstate = Encoder_Interface_init(dtx);


    /* write magic number to indicate single channel AMR file storage format */
    bytes = fwrite(AMR_MAGIC_NUMBER, sizeof(char), strlen(AMR_MAGIC_NUMBER), file_encoded);

    /* read file */
    while (fread( speech, sizeof (Word16), 160, file_speech ) > 0)
    {
        /* read mode */
        if (file_mode != NULL) {
            req_mode = 8;
            if (fscanf(file_mode, "%9s\n", mode_string) != EOF) {
                mode_tmp = strtol(&mode_string[2], NULL, 0);
                for (req_mode = 0; req_mode < 8; req_mode++) {
                    if (mode_tmp == modeConv[req_mode]) {
                        break;
                    }
                }
            }
            if (req_mode == 8) {
                break;
            }
        }

        frames ++;

        /* call encoder */
        byte_counter = Encoder_Interface_Encode(enstate, req_mode, speech, serial_data, 0);

        bytes += byte_counter;
        fwrite(serial_data, sizeof (UWord8), byte_counter, file_encoded );
        fflush(file_encoded);
    }
    Encoder_Interface_exit(enstate);

    fprintf ( stderr, "\n%s%i%s%i%s\n", "Frame structure AMR MIME file storage format: ", frames, " frames, ", bytes, " bytes.");

    fclose(file_speech);
    fclose(file_encoded);
    if (file_mode != NULL)
        fclose(file_mode);

    return 0;
}
